package com.atmilan.quartz.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.atmilan.quartz.model.entity.buyer.Supplier;
import com.atmilan.quartz.model.entity.crmeb.ChannelMerchant;
import com.atmilan.quartz.service.IChannelMerchantService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.constant.Constants;
import com.atmilan.quartz.model.entity.buyer.MoneyExchange;
import com.atmilan.quartz.model.result.PriceResult;
import com.atmilan.quartz.service.IMoneyExchangeService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

/**
 *
 * 价格计算工具
 * @author muyu.myw
 * @date 2020/9/28 11:46
 */
@Service
public class PriceUtil {
    public static final String DEFAULT_EXCHANGE_RATE_EUR = "8";
    public static final String DEFAULT_EXCHANGE_RATE_US = "6.5";
    public static final String DEFAULT_EXCHANGE_RATE_HK = "86";
    public static final String DEFAULT_EXCHANGE_RATE_EUR_HK = "950";

    static final String eurName = "欧元";
    static final String hkName = "港币";
    @Autowired
    private IMoneyExchangeService moneyExchangeService;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IChannelMerchantService channelMerchantService;

    /**
     * 美元转人民币
     * @param otPrice
     * @return
     */
    public static BigDecimal us2Rmb(BigDecimal otPrice) {
        BigDecimal us2Rmb = new BigDecimal(DEFAULT_EXCHANGE_RATE_US);
        return otPrice.multiply(us2Rmb).setScale(2, RoundingMode.UP);
    }
    
    /**
     * 欧元转人民币(动态汇率)
     * @param
     * @return
     */
    public PriceResult eur2Rmb(BigDecimal price) {
    	// 缓存读取汇率
        Object eurRate = redisUtil.get(Constants.EXCHANGE_EUR_KEY);
        if(null == eurRate) {
            MoneyExchange moneyExchange = moneyExchangeService.getExchangeByName(Constants.EXCHANGE_EUR_NAME);
            eurRate = null == moneyExchange ? DEFAULT_EXCHANGE_RATE_EUR : moneyExchange.getBankConversionPri();
            redisUtil.set(Constants.EXCHANGE_EUR_KEY, eurRate, 2L, TimeUnit.HOURS);
        }
    	
    	BigDecimal eur2Rmb = new BigDecimal(eurRate.toString());
        
        return new PriceResult(price.multiply(eur2Rmb).setScale(2, RoundingMode.UP), eur2Rmb);
    }
    
    /**
     * 欧元转人民币(静态汇率)
     * @param otPrice
     * @return
     */
    public BigDecimal eur2RmbStatic(BigDecimal otPrice) {
    	if(null == otPrice) {
    		return BigDecimal.ZERO;
    	}
    	BigDecimal eur2Rmb = new BigDecimal(DEFAULT_EXCHANGE_RATE_EUR);
    	return otPrice.multiply(eur2Rmb).setScale(2, RoundingMode.UP);
    }
    
    /**
     * 人民币转欧元(动态汇率)
     * @Description:  
     * @author daigb 
     * @param @param otPrice
     * @param @return
     * @return BigDecimal
     * @throws
     */
    public PriceResult rmb2Eur(BigDecimal price) {
    	// 缓存读取汇率
    	Object eurRate = redisUtil.get(Constants.EXCHANGE_EUR_KEY);
    	if(null == eurRate) {
    		MoneyExchange moneyExchange = moneyExchangeService.getExchangeByName(Constants.EXCHANGE_EUR_NAME);
    		eurRate = null == moneyExchange ? DEFAULT_EXCHANGE_RATE_EUR : moneyExchange.getBankConversionPri();
			redisUtil.set(Constants.EXCHANGE_EUR_KEY, eurRate, 2L, TimeUnit.HOURS);
    	}
    	if(0 == BigDecimal.ZERO.compareTo(price)) {
            return new PriceResult(price, new BigDecimal(eurRate.toString()));
    	}

        return new PriceResult(price.divide(new BigDecimal(eurRate.toString()), 2, RoundingMode.HALF_UP), new BigDecimal(eurRate.toString()));
    }
    
    /**
     * 人民币转欧元(静态汇率)
     * @Description:  
     * @author daigb 
     * @param @param otPrice
     * @param @return
     * @return BigDecimal
     * @throws
     */
    public BigDecimal rmb2EurStatic(BigDecimal otPrice) {
    	if(null == otPrice) {
    		return BigDecimal.ZERO;
    	}
    	BigDecimal rmb2Eur = new BigDecimal(DEFAULT_EXCHANGE_RATE_EUR);
    	return otPrice.divide(rmb2Eur, 2,RoundingMode.HALF_UP);
    }

    /**
     * 港元转人民币
     * @param otPrice
     * @return
     */
    public static BigDecimal hk2Rmb(BigDecimal otPrice) {
        //        MoneyExchange moneyExchange = moneyExchangeService.getExchangeByName(hkName);
        // NumberUtil.toBigDecimal(moneyExchange != null?moneyExchange.getBankConversionPri():Constants.DEFAULT_EXCHANGE_RATE_HK);
        BigDecimal hk2Rmb = new BigDecimal(DEFAULT_EXCHANGE_RATE_HK);
        return otPrice.multiply(hk2Rmb).movePointLeft(2).setScale(2, RoundingMode.UP);
    }

    /**
     * 欧元转人民币
     * @param otPrice
     * @return
     */
    public static BigDecimal hkStr2Rmb(String otPrice) {
        if (StringUtils.isBlank(otPrice)) {
            return new BigDecimal(0);
        }
        BigDecimal hk2RmbRate = new BigDecimal(DEFAULT_EXCHANGE_RATE_HK);
        BigDecimal rmbPrice = new BigDecimal(otPrice).multiply(hk2RmbRate).movePointLeft(2).setScale(2, RoundingMode.UP);
        return rmbPrice;
    }

    /**
     * 价格计算
     * @param price
     * @param rate   利润浮动
     * @return
     */
    public static BigDecimal calePrice(BigDecimal price, String rate) {
        // 售价 , 在buyer的基础上下浮 1.5%
        price = price.multiply(new BigDecimal(rate)).setScale(2, RoundingMode.HALF_UP);
        // 对个位的位数进行整理,让数字更好看 , 1234向下取整未0, 567 向上取8, 9,0不变
        int lp = price.intValue() % 10;
        switch (lp) {
        case 1:
        case 2:
        case 3:
        case 4:
            price = price.subtract(new BigDecimal(lp));
            break;
        case 5:
        case 6:
        case 7:
            price = price.add(new BigDecimal(8 - lp));
        }
        return price;
    }

    /**
     * 人民币转不同币种(动态汇率)
     * @param price
     * @return
     */
    public PriceResult rmb2Currency(BigDecimal price,Integer moneyExchangeId){

        if (price == null){
            return new PriceResult(BigDecimal.ZERO, BigDecimal.ZERO);
        }

        BigDecimal renewal_price  =  getMoneyExchange(moneyExchangeId);

//        BigDecimal price1 = price.divide(renewal_price, 2, RoundingMode.HALF_UP);

        return new PriceResult(price.divide(renewal_price,2, RoundingMode.HALF_UP), renewal_price);

    }

    /**
     * 人民币转不同币种(动态汇率)
     * @param price
     * @return
     */
    public PriceResult rmb2Currency(BigDecimal price, Supplier supplier){

        if (price == null){
            return new PriceResult(BigDecimal.ZERO, BigDecimal.ZERO);
        }

        if (Objects.nonNull(supplier)) {

            BigDecimal renewal_price  =  getMoneyExchange(supplier.getCurrencyId());

            return new PriceResult(price.divide(renewal_price,2, RoundingMode.HALF_UP), renewal_price);
        }else
        {
            return new PriceResult(price.divide( new BigDecimal(DEFAULT_EXCHANGE_RATE_EUR),2, RoundingMode.HALF_UP), new BigDecimal(DEFAULT_EXCHANGE_RATE_EUR));
        }

    }

    /**
     * 不同币种转人民币(动态汇率)
     * @param price
     * @return
     */
    public PriceResult currency2Rmb(BigDecimal price,Integer moneyExchangeId){

        if (price == null){
            return new PriceResult(BigDecimal.ZERO, BigDecimal.ZERO);
        }

        BigDecimal renewal_price  =  getMoneyExchange(moneyExchangeId);

        return new PriceResult(price.multiply(renewal_price).setScale(2, RoundingMode.UP), renewal_price);
    }

    /**
     * 获取不同币种的汇率
     * @param id
     * @return 运费
     */
    public BigDecimal getMoneyExchange(Integer id) {

        if (id == 0) {
            id =Constants.DEFAULT_CURRENCY_ID;
        }

        MoneyExchange moneyExchange = moneyExchangeService.getById(id);

        if (Objects.isNull(moneyExchange)){
            return  new BigDecimal(DEFAULT_EXCHANGE_RATE_EUR) ;
        }else
        {
            BigDecimal money_exchange = new BigDecimal(moneyExchange.getBankConversionPri());
            return  money_exchange;

        }

    }

    /**
     * 通过供应商id获取供应商信息
     * @param supplierId
     * @return ChannelMerchant
     */
    public ChannelMerchant getChannelMerchantBySupplierId(Integer supplierId) {

        ChannelMerchant channelMerchantSupplier = channelMerchantService.getOne(Wrappers.<ChannelMerchant>lambdaQuery()
                .eq(ChannelMerchant::getChannelType, 3)
                .eq(ChannelMerchant::getSuppliersIds, "[" + supplierId + "]")
                .last("LIMIT 1"));

        return channelMerchantSupplier;
    }


}
